﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace VisualSearch.Core
{
    class ImageHandler : System.Web.IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return false; }
        }

        public bool ThumbnailCallback()
        {
            return false;
        }
        public void ProcessRequest(System.Web.HttpContext context)
        {
            var id = context.Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                context.Response.ContentEncoding = Encoding.Default;
                context.Response.ContentType = "image/jpeg";
                var provider = SearchFactory.CreateProvider();
                var photo = provider.GetPhotoStream(id);
                //var sourcePhoto = new MemoryStream(photo);
                //var image = new System.Drawing.Bitmap(sourcePhoto);
                //System.Drawing.Image.GetThumbnailImageAbort myCallback = ThumbnailCallback;

                //var thrumbnail = image.GetThumbnailImage(32, 36, myCallback, IntPtr.Zero);
                //var destinationPhoto = new MemoryStream();
                //thrumbnail.Save(destinationPhoto, System.Drawing.Imaging.ImageFormat.Gif);
                System.IO.MemoryStream memoryStream = new MemoryStream(photo);
                memoryStream.WriteTo(context.Response.OutputStream);
                //Array.ForEach(photo, p => context.Response.Write((char)p));
                //context.Response.Flush();
            }
        }

        #endregion
    }
}
